package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.woniuhis.entity.Office;
import com.woniuxy.woniuhis.exception.OfficeServiceException;
import com.woniuxy.woniuhis.service.OfficeService;
import com.woniuxy.woniuhis.util.ResponseInfo;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
@RequestMapping("/office")
public class OfficeController {

	@Autowired
	OfficeService officeService;

	/**
	 * 	分页查询所有科室信息
	 * 
	 * @param office
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findOffices")
	public ResponseJson<Office> findOffices(
			Office office,
			@RequestParam(value = "page",defaultValue = "1",required = false) Integer pageIndex,
			@RequestParam(value = "limit",defaultValue = "5",required = false) Integer pageSize) {
		if(office == null && StringUtils.isEmpty(office.getOfficeId())
				&& StringUtils.isEmpty(office.getOfficeName()) ) {
			office = new Office();
		}
		ResponseJson<Office> respjson = new ResponseJson<Office>();
		try {
			PageInfo<Office> offices = officeService.findAllOffices(office,pageIndex,pageSize);
			respjson.setCode(200);
			respjson.setData(offices.getList());
			respjson.setCount(offices.getSize());
			return respjson;
		} catch (OfficeServiceException e) {
			e.printStackTrace();
			respjson.setCode(500);
			respjson.setMsg("没有查询到任何科室信息");
			return respjson;
		}
	}
	@ResponseBody
	@GetMapping("/getAllOffices")
	public ResponseJson<Office> findOffices(){
		ResponseJson<Office> respjson = new ResponseJson<Office>();
		try {
			List<Office> list = officeService.findAllOffices();
			respjson.setCode(200);
			respjson.setData(list);
			respjson.setCount(list.size());
			return respjson;
		} catch (OfficeServiceException e) {
			e.printStackTrace();
			respjson.setCode(500);
			respjson.setMsg("没有查询到任何科室信息");
			return respjson;
		}
	}
	/**
	 * 	添加新科室
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addOffice")
	public ResponseJson<Office> addOffice(HttpServletRequest req) {
		Office office = new Office();
		office.setOfficeId(req.getParameter("officeId"));
		office.setOfficeName(req.getParameter("officeName"));
		try {
			officeService.addOffice(office);
			ResponseJson<Office> respJson = new ResponseJson<Office>();
			respJson.setCode(200);
			respJson.setMsg("添加成功");
			return respJson;
		} catch (OfficeServiceException e) {
			e.printStackTrace();
			ResponseJson<Office> respJson = new ResponseJson<Office>();
			respJson.setCode(200);
			respJson.setMsg("添加失败");
			return respJson;
		}
	}

	/**
	 * 根据主键查找科室信息
	 * 
	 * @param req
	 * @return
	 */
	String browserOfficeId;

	@ResponseBody
	@RequestMapping(value = "/findOfficeToUpdate")
	public ModelAndView findOfficeToUpdate(HttpServletRequest req) {
		browserOfficeId = req.getParameter("officeId");
		try {
			Office office = officeService.findOfficeInfoByofficeId(browserOfficeId);
			ModelAndView mv = new ModelAndView();
			mv.addObject("office", office);
			mv.setViewName("views/system/updateOfficeInfo.html");
			return mv;
		} catch (OfficeServiceException e) {
			e.printStackTrace();
			ModelAndView mv = new ModelAndView();
			mv.addObject("msg", "跳转失败");
			mv.setViewName("msg.html");
			return mv;
		}

	}

	/**
	 * 修改科室信息
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateOffice")
	public ResponseInfo updateOffice(HttpServletRequest req) {
		Office office = new Office();
		String officeName = req.getParameter("officeName");
		if (browserOfficeId != null && officeName != null) {
			office.setOfficeId(browserOfficeId);
			office.setOfficeName(officeName);
		}
		try {
			officeService.modifyOfficeInfo(office);
			ResponseInfo respInfo = new ResponseInfo();
			respInfo.setMsg("科室信息修改成功");
			return respInfo;
		} catch (OfficeServiceException e) {
			e.printStackTrace();
			ResponseInfo respInfo = new ResponseInfo();
			respInfo.setMsg("科室信息修改失败");
			return respInfo;
		}
	}

	/**
	 * 按主键删除科室信息
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delOfficeInfo")
	public ModelAndView delOfficeInfo(HttpServletRequest req) {
		String officeId = req.getParameter("officeId");

		try {
			officeService.delOfficeByofficeId(officeId);
			ModelAndView mv = new ModelAndView();
			mv.addObject("msg", "科室信息删除成功");
			mv.setViewName("msg.html");
			return mv;
		} catch (OfficeServiceException e) {
			e.printStackTrace();
			ModelAndView mv = new ModelAndView();
			mv.addObject("msg", "科室信息删除失败");
			mv.setViewName("msg.html");
			return mv;
		}
	}

}
